﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;

namespace SuperKit {
    public class KFile {
        public const int KB = 1024;
        public const int MB = 1024 * 1024;



        public static List<string> ReadFileLines(string path) {
            List<string> lines = new List<string>();
            
            try {
                FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);//读取文件设定
                StreamReader m_streamReader = new StreamReader(fs, System.Text.Encoding.GetEncoding("GB2312"));//设定读写的编码
                                                                                                               //使用StreamReader类来读取文件
                m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin);


                string strLine = m_streamReader.ReadLine();
                while (strLine != null) {
                    lines.Add(strLine);
                    strLine = m_streamReader.ReadLine();
                }
                //关闭此StreamReader对象
                m_streamReader.Close();
            } catch {
               
            }
            return lines;
        }

        
        public static string ReplaceExtension(string file, string extension) {
            // 确保无论输入的扩展名是否带有点号（例如：“.jpg”还是“jpg”）都有效。
            extension = "." + extension.Trim('.');
            var name = Path.GetFileNameWithoutExtension(file);
            var dir = Path.GetDirectoryName(file);
            return Path.Combine(dir, name + extension);
        }

        public static string AddSerialID(string file, int index) {
            // 确保无论输入的扩展名是否带有点号（例如：“.jpg”还是“jpg”）都有效。
            var ext = Path.GetExtension(file);
            var name = Path.GetFileNameWithoutExtension(file);
            var dir = Path.GetDirectoryName(file);
            return Path.Combine(dir, string.Format("{0}_{1}{2}", name, index, ext));
        }

        private const int FO_DELETE = 0x3;
        private const ushort FOF_NOCONFIRMATION = 0x10;
        private const ushort FOF_ALLOWUNDO = 0x40;

        [DllImport("shell32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
        private static extern int SHFileOperation([In, Out] _SHFILEOPSTRUCT str);

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
        public class _SHFILEOPSTRUCT {
            public IntPtr hwnd;
            public UInt32 wFunc;
            public string pFrom;
            public string pTo;
            public UInt16 fFlags;
            public Int32 fAnyOperationsAborted;
            public IntPtr hNameMappings;
            public string lpszProgressTitle;
        }
        /// <summary>
        /// 移动到回收站中
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static int MoveToTrash(string path) {
            _SHFILEOPSTRUCT pm = new _SHFILEOPSTRUCT();
            pm.wFunc = FO_DELETE;
            pm.pFrom = path + '\0';
            pm.pTo = null;
            pm.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION;
            return SHFileOperation(pm);
        }

        public static int GetAllFileCount(string path) {
            // 如果是文件，则为1
            if (File.Exists(path)) {
                return 1;
            }

            if (Directory.Exists(path)) {
                // 得到文件和子文件
                DirectoryInfo dir = new DirectoryInfo(path);
                int count = Kit.Len(dir.GetFiles());
                var dirs = dir.GetDirectories();
                count += Kit.Len(dirs);
                foreach (var sub in dirs) {
                    count += GetAllFileCount(sub.FullName);
                }
                return count;
            }

            return 0;

        }
    }
}
